#!/bin/bash

get_char()
        {
        SAVEDSTTY=`stty -g`
        stty -echo
        stty cbreak
        dd if=/dev/tty bs=1 count=1 2> /dev/null
        stty -raw
        stty echo
        stty $SAVEDSTTY
        }

echo "此脚本用于将IPA上传至www.pgyer.com和BuildBrowser，并下载二维码。（当前路径不得有空格）"
echo "请输入版本号（例如0.4.2）： "
read Version
echo "请输入build号（例如31）： "
read BuildNumber
#echo "请输入要放人buildbrowser的分支，例如main或rel_1.2.0 : "
#read Branch

BuildDir="${Branch}/${Version}/${Version}#${BuildNumber}-`date +%y%m%d%H%M`"/"ipa"
BuildDir="${Version}/${Version}#${BuildNumber}-`date +%y%m%d%H%M`"/
BuildName="v${Version}_B${BuildNumber}-`date +%Y%m%d`.ipa"
cd `dirname $0`
pwd
mkdir -p ${BuildDir}/{ipa,QRCode}

#上传至蒲公英并下载二维码

if [[ -d 531app ]]; then
        cd 531app
        curl -F "file=@Unity-iPhone.ipa" -F "uKey=f6e53bf36e08f8f15f73f3efa6d65fa4" -F "_api_key=16c38543300528d47c93a405f52396c2" http://www.pgyer.com/apiv1/app/upload | awk -F \" '{print $80}' | xargs curl -o QRCode_Unity531_v${Version}_B${BuildNumber}.jpg
        mv Unity-iPhone.ipa ../${BuildDir}/ipa/Unity531_${BuildName}
        mv QRCode_Unity531_v${Version}_B${BuildNumber}.jpg ../${BuildDir}/QRCode/
        cd ../
fi

if [[ -d 532app ]]; then
        cd 532app
        curl -F "file=@Unity-iPhone.ipa" -F "uKey=f6e53bf36e08f8f15f73f3efa6d65fa4" -F "_api_key=16c38543300528d47c93a405f52396c2" http://www.pgyer.com/apiv1/app/upload | awk -F \" '{print $80}' | xargs curl -o QRCode_Unity532_v${Version}_B${BuildNumber}.jpg
        mv Unity-iPhone.ipa ../${BuildDir}/ipa/Unity532_${BuildName}
        mv QRCode_Unity532_v${Version}_B${BuildNumber}.jpg ../${BuildDir}/QRCode/
        cd ../
fi

if [[ -d 533app ]]; then
        cd 533app
        curl -F "file=@Unity-iPhone.ipa" -F "uKey=f6e53bf36e08f8f15f73f3efa6d65fa4" -F "_api_key=16c38543300528d47c93a405f52396c2" http://www.pgyer.com/apiv1/app/upload | awk -F \" '{print $80}' | xargs curl -o QRCode_Unity533_v${Version}_B${BuildNumber}.jpg
        mv Unity-iPhone.ipa ../${BuildDir}/ipa/Unity533_${BuildName}
        mv QRCode_Unity533_v${Version}_B${BuildNumber}.jpg ../${BuildDir}/QRCode/
        cd ../
fi

if [[ -d 534app ]]; then
        cd 534app
        curl -F "file=@Unity-iPhone.ipa" -F "uKey=f6e53bf36e08f8f15f73f3efa6d65fa4" -F "_api_key=16c38543300528d47c93a405f52396c2" http://www.pgyer.com/apiv1/app/upload | awk -F \" '{print $80}' | xargs curl -o QRCode_Unity534_v${Version}_B${BuildNumber}.jpg
        mv Unity-iPhone.ipa ../${BuildDir}/ipa/Unity534_${BuildName}
        mv QRCode_Unity534_v${Version}_B${BuildNumber}.jpg ../${BuildDir}/QRCode/
        cd ../
fi

if [[ -d 535app ]]; then
        cd 535app
        curl -F "file=@Unity-iPhone.ipa" -F "uKey=f6e53bf36e08f8f15f73f3efa6d65fa4" -F "_api_key=16c38543300528d47c93a405f52396c2" http://www.pgyer.com/apiv1/app/upload | awk -F \" '{print $80}' | xargs curl -o QRCode_Unity535_v${Version}_B${BuildNumber}.jpg
        mv Unity-iPhone.ipa ../${BuildDir}/ipa/Unity535_${BuildName}
        mv QRCode_Unity535_v${Version}_B${BuildNumber}.jpg ../${BuildDir}/QRCode/
        cd ../
fi

cd ${BuildDir}/QRCode/
if [[ -f QRCode_Unity531_v${Version}_B${BuildNumber}.jpg && -f QRCode_Unity532_v${Version}_B${BuildNumber}.jpg && -f QRCode_Unity533_v${Version}_B${BuildNumber}.jpg && -f QRCode_Unity534_v${Version}_B${BuildNumber}.jpg && -f QRCode_Unity535_v${Version}_B${BuildNumber}.jpg ]]; then
	echo "5个版本的app成功上传至蒲公英并下载二维码：QRCode_Unity53｛1-5｝_v${Version}_B${BuildNumber}.jpg"
else
	echo "failed！有文件未能成功上传至蒲公英，请检查！"
fi

cd `dirname $0`
pwd
#上传至BuildBrowser
lftp << EOF
open ftp://neil.qiao:qiao@Pico123@10.10.194.14:8021
#get /scmbuilds/software/git相关/repo
cd scmbuilds/qa/sdk/iOS_SDK_DEMO/main
#put test.exe 
mirror -R ${Version}/
close
bye
EOF

if [[ $? -eq 0 ]] ; then
        echo "文件已上传至BuildBorwser！"
else
        echo "failed！文件上传至BuildBorwser失败，请检查！"
fi

##上传二维码只青岛ftp的temp目录
#lftp << EOF
#open ftp://neil.qiao:qiao@Pico123@192.168.97.85:8021
##get /scmbuilds/software/git相关/repo
#cd scmbuilds/temp/
#put QRCode_v${Version}_B${BuildNumber}.jpg 
##mirror -R ${Version}/
#close
#bye
#EOF

echo "任意键继续..."
char=`get_char`

